﻿using System;
using System.Collections.Generic;
using System.Text;
using ProtocolVN.Framework.Core;
using DevExpress.XtraGrid.Columns;

namespace ProtocolVN.Framework.Win
{
    
    public class PhieuQLCustomChangeFix
    {
        private PhieuQLCustomChange that;
        private bool DuyetSupport = false;
        private bool find=false; 
        public PhieuQLCustomChangeFix(PhieuQLCustomChange phieuQL) {
            this.find = false;
            this.DuyetSupport = false;
            this.that = phieuQL;
            if (phieuQL is IDuyetSupport)
            {
                this.DuyetSupport = true;
            }
            this.HamDung();
        }
        public void HamDung() {
            this.that.MainBar.Text ="Chức năng" ;
            this.that.MainBar.Manager.AllowCustomization = false;
            this.that.MainBar.OptionsBar.AllowDelete = false;
            this.that.MainBar.OptionsBar.DisableClose = false;
            this.that.MainBar.OptionsBar.DisableCustomization = false;
            this.that.barButtonItemClose.Visibility =  DevExpress.XtraBars.BarItemVisibility.Never;
            this.that.barButtonItemAdd.Glyph= FWImageDic.ADD_IMAGE20;
            this.that.barButtonItemXem.Glyph =FWImageDic.VIEW_IMAGE20;
            this.that.barButtonItemDelete.Glyph = FWImageDic.DELETE_IMAGE20;
            this.that.barButtonItemUpdate.Glyph = FWImageDic.EDIT_IMAGE20;
            this.that.barButtonItemPrint.Glyph = FWImageDic.PRINT_IMAGE20;
            this.that.barButtonItemClose.Glyph = FWImageDic.CLOSE_IMAGE20;
            this.that.barButtonItemCommit.Glyph = FWImageDic.COMMIT_IMAGE20;
            this.that.barButtonItemNoCommit.Glyph = FWImageDic.UNCOMMIT_IMAGE20;
            this.that.barSubItem1.Glyph = FWImageDic.BUSINESS_IMAGE20;
            this.that.barButtonItemSearch.Glyph = FWImageDic.FIND_IMAGE20;
            this.that.barCheckItemFilter.Glyph = FWImageDic.FILTER_IMAGE20;
            try
            {
                this.that.InitMaster();
                this.that.InitDetail();              
            }
            catch (Exception exception)
            {
                PLException.AddException(exception);
                HelpDevError.ShowMessage(this, exception.Message);
            }
            if (this.DuyetSupport)
            {
                GridColumn col = new GridColumn();
                XtraGridSupportExt.CreateDuyetGridColumn(col);
                //this.that.gridViewMaster.get_Columns().Add(col);
            }
            else
            {
                this.that.barButtonItemCommit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.that.barButtonItemNoCommit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
            //if (!this.that.InSupport)
            //{
            //    this.that.barButtonItemPrint.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            //}
            //else
            //{
                this.that.barButtonItemPrint.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemPrint_ItemClick); 
            //}
            this.that.barButtonItem4.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItem4_ItemClick);
            this.that.barButtonItemAdd.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemAdd_ItemClick);
            this.that.barButtonItemXem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemXem_ItemClick);
            this.that.barButtonItemDelete.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemDelete_ItemClick);
            this.that.barButtonItemUpdate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemUpdate_ItemClick);
            this.that.barButtonItemCommit.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemCommit_ItemClick);
            this.that.barButtonItemNoCommit.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemNoCommit_ItemClick);
            this.that.barButtonItemSearch.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemSearch_ItemClick);
            this.that.barCheckItemFilter.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barCheckItemFilter_ItemClick);
            this.that.barButtonItemClose.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(barButtonItemClose_ItemClick);
            this.that.splitContainerControl1.Resize += new EventHandler(splitContainerControl1_Resize);
            this.that.gridViewMaster.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewMaster_FocusedRowChanged);
            //this.that.gridViewMaster.add_FocusedRowChanged(new FocusedRowChangedEventHandler(this, (IntPtr)this.gridViewMaster_FocusedRowChanged));
            //this.that.gridViewMaster.add_DoubleClick(new EventHandler(this.gridViewMaster_DoubleClick));
            this.that.Load += new EventHandler(this.PhieuQL_Load);
            //if (this.that._PLAutoWidth.HasValue && (this.that._PLAutoWidth == true))
            //{
            //    this.that.gridViewDetail._SetAutoLayout();
            //    this.that.gridViewMaster._SetAutoLayout();
            //}

        }

        void gridViewMaster_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.that.HookFocusRow();
        }

        void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //Xem trươc khi in
        }
        public void PhieuQL_Load(object sender, EventArgs e) { 

        }
        public void splitContainerControl1_Resize(object sender, EventArgs e)
        {
            
        }

        public void barButtonItemClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        public void barCheckItemFilter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        public void barButtonItemSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.that.Find(this.that.PLBuildQueryFilter());           
        }

        public void barButtonItemNoCommit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        public void barButtonItemCommit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        public void barButtonItemUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (this.that.GetIDKey()!=-1)
                {
                    long id = this.that.GetIDKey();
                    this.that.ShowUpdateForm(id);
                    //if (!this.UpdateRow() && (((this.that.filter != null) && !this.that.filter.isEmpty()) && this.find))
                    //{
                    //    this.PLRefresh();
                    //}
                }
            }
            catch (Exception exception)
            {
                PLException.AddException(exception);
            }

        }

        public void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.that.XoaAction(this.that.GetIDKey());
            this.that.Find(this.that.PLBuildQueryFilter());  
        }

        public void barButtonItemXem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                if (this.that.GetIDKey()!=-1)
                {
                    this.that.ShowViewForm(this.that.GetIDKey());
                }
                else
                {
                    PLMessageBox.ShowNotificationMessage("Chưa chọn dữ liệu.");
                }
            }
            catch (Exception exception)
            {
                PLException.AddException(exception);
            }

        }

        public void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                long[] numArray = this.that.ShowAddForm();
                //if (numArray != null)
                //{
                //    string queryHeader = this.that.UpdateRow();
                //    if ((queryHeader != null) && (queryHeader != ""))
                //    {
                //        DataTable table = ((DataTable)this.that.gridControlMaster.get_DataSource()).DataSet.Tables[0];
                //        for (int i = 0; i < numArray.Length; i++)
                //        {
                //            QueryBuilder query = new QueryBuilder(queryHeader);
                //            query.addID(this.that.IDField, numArray[i]);
                //            DataSet set = DABase.getDatabase().LoadReadOnlyDataSet(query);
                //            if (set.Tables[0].Rows.Count == 1)
                //            {
                //                DataRow row = table.NewRow();
                //                row.ItemArray = set.Tables[0].Rows[0].ItemArray;
                //                table.Rows.Add(row);
                //            }
                //        }
                //    }
                //}
            }
            catch (Exception exception)
            {
                PLException.AddException(exception);
            }

        }

        public void barButtonItemPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //In
        }
    }
}
